package com.kaluli.project.repository.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Repository;

import com.kaluli.project.domain.entity.KllMq;
import com.kaluli.project.domain.entity.KllMqBak;
import com.kaluli.project.domain.repository.KllMqRepository;
import com.kaluli.project.utils.DateOprator;
import com.kaluli.project.utils.DateUtil;
import com.kaluli.system.repository.AbstractRepositorySupport;

@Repository
public class KllMqRepositoryImpl extends AbstractRepositorySupport<KllMq>
		implements KllMqRepository{

	@Override
	public List<KllMq> getAll() {
		StringBuffer hql=new StringBuffer(" from KllMq t ");
		Map<String,Object> params=new HashMap<String, Object>();
		return this.findHqlForMap(hql.toString(), params);
	}
	

	public List<KllMq> findMoveList() {
		Date time = DateOprator.getPrevMonthByLength(new Date(), 1);
		StringBuffer hql=new StringBuffer(" from KllMq t where t.msg_time<'"+DateUtils.formatDate(time, "yyyy-MM-dd HH:MM:ss")+"'");
		Map<String,Object> params=new HashMap<String, Object>();
		return this.findHqlForMap(hql.toString(), params);
	}
	
	@Override
	public List<KllMq> getByOrderNumber(String orderNumber,String msgChannel) {
		StringBuffer hql=new StringBuffer(" from KllMq t where t.order_number=:order_number and t.msg_channel=:msg_channel ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("order_number", orderNumber);
		params.put("msg_channel",msgChannel);
		return this.findHqlForMap(hql.toString(), params);
	}
	
	public List<KllMq> findByOrderNumber(String orderNumber) {
		StringBuffer hql=new StringBuffer(" from KllMq t where t.order_number=:order_number ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("order_number", orderNumber);
		return this.findHqlForMap(hql.toString(), params);
	}
	
	public List<KllMq> findMqOrderNumber(String orderNumber,String msgChannel) {
		StringBuffer hql=new StringBuffer(" from KllMq t where t.order_number=:order_number and t.msg_channel=:msg_channel and t.msg_status=1 order by t.msg_time desc");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("order_number", orderNumber);
		params.put("msg_channel",msgChannel);
		return this.findHqlForMap(hql.toString(), params);
	}
	
	public List<KllMq> findMqlistOnehour() {
		StringBuffer hql=new StringBuffer(" from KllMq t where t.msg_time > :dateHour and t.msg_time <:dateNow and t.msg_channel=:msgChannel ");
		Map<String,Object> params=new HashMap<String, Object>();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		params.put("dateHour", DateUtil.beforeOneHourToNowDate());
		params.put("dateNow",new Date());
		params.put("msgChannel", "AlipayGj");
		return this.findHqlForMap(hql.toString(), params);
	}

	@Override
	protected Class<KllMq> getEntityClass() {
		// TODO 自动生成的方法存根
		return KllMq.class;
	}

	@Override
	public void updateMq(KllMq kllMq){
		StringBuffer hql_select = new StringBuffer(" from KllMq t where t.msg_id=:msg_id and t.order_number=:order_number and t.msg_channel=:msg_channel");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("msg_id", kllMq.getMsg_id());
		params.put("order_number",kllMq.getOrder_number());
		params.put("msg_channel", kllMq.getMsg_channel());
		List<KllMq> lists =  this.findHqlForMap(hql_select.toString(), params);
		if(lists!=null&&lists.size()>0){
			String msgId = lists.get(0).getMsg_id();
			StringBuffer hql = new StringBuffer("update KllMq t set t.msg_time=?,t.msg_response=?,t.msg_status=? where t.msg_id=?");
			Object[] param = {new Timestamp(System.currentTimeMillis()),kllMq.getMsg_response(),kllMq.getMsg_status(),msgId};
			this.executeHql(hql.toString(), param);
		}else{
			StringBuffer hql = new StringBuffer("update KllMq t set t.msg_time=?,t.msg_response=?,t.msg_status=? where t.msg_id=? and order_number=? and t.msg_channel=?");
			Object[] param = {new Timestamp(System.currentTimeMillis()),kllMq.getMsg_response(),kllMq.getMsg_status(),kllMq.getMsg_id(),kllMq.getOrder_number(),kllMq.getMsg_channel()};
			this.executeHql(hql.toString(), param);
		}
		
		
		
	}

	@Override
	public KllMq findByMsgId(String msgId) {
		KllMq kllMq = null;
		StringBuffer hql=new StringBuffer(" from KllMq t where t.msg_id=:msg_id ");
		Map<String,Object> params=new HashMap<String, Object>();
		params.put("msg_id", msgId);
		List<KllMq> lists =  this.findHqlForMap(hql.toString(), params);
		if(lists!=null&&lists.size()>0){
			kllMq = lists.get(0);
		}
		
		return kllMq;
	}

}
